package com.tao.camera

import android.content.Context
import android.graphics.Point
import android.hardware.Camera
import android.os.Handler
import android.os.Looper
import android.view.View

/**
 * 摄像头操作类
 */
class CameraOpHelper {
    var cameraIds = arrayOf(Camera.CameraInfo.CAMERA_FACING_BACK, Camera.CameraInfo.CAMERA_FACING_FRONT)

    companion object {
        private val instance: CameraOpHelper = CameraOpHelper()

        @JvmStatic
        fun instance(): CameraOpHelper {
            return instance
        }
    }

    /**
     * 初始化摄像头基本参数
     */
    fun initCameraPer(context: Context, perviewSize: Point) {
        CameraManager.getInstance().init(context).initCameraConfig(cameraIds[0], perviewSize, null)
        CameraManager.getInstance().init(context).initCameraConfig(cameraIds[1], perviewSize, null)
    }

    fun switcCameraId(switchId: Boolean): CameraOpHelper {
        if (switchId) {
            cameraIds = arrayOf(Camera.CameraInfo.CAMERA_FACING_FRONT, Camera.CameraInfo.CAMERA_FACING_BACK)
        } else {
            cameraIds = arrayOf(Camera.CameraInfo.CAMERA_FACING_BACK, Camera.CameraInfo.CAMERA_FACING_FRONT)

        }
        return this
    }


    /**
     * 切换摄像头预览的view
     *
     */
    fun switchCameraPerviewView(view: View, front: Boolean) {
        switchCameraPerviewView(view, front, false)
    }

    fun switchCameraPerviewView(view: View, front: Boolean, mirror: Boolean) {
//        val perviewIng = CameraManager.getInstance().getOrCreateHelperWithId(cameraId).isPerviewIng
        CameraManager.getInstance().switchPerviewView(getCameraId(front), view, mirror)

    }

    public fun getCameraId(front: Boolean): Int {
        return if (front) cameraIds[0] else cameraIds[1]
    }


    fun addCameraListener(front: Boolean, cameraListener: CameraListener) {
        cameraListener?.let {
            CameraManager.getInstance().addExterCameraListener(getCameraId(front), cameraListener)
        }
    }

    fun removeCameraListener(front: Boolean, cameraListener: CameraListener?) {
        cameraListener?.let {
            CameraManager.getInstance().reMoveExterCameraListener(getCameraId(front), cameraListener)
        }

        if (isPerviewIng(getCameraId(front)) && listenerSize(getCameraId(front)) == 0) {
            delyCloseCamera(getCameraId(front))
        }
    }

    fun clearCameraListener(front: Boolean) {
        CameraManager.getInstance().clearCameraListener(getCameraId(front))
        delyCloseCamera(getCameraId(front))
    }

    fun listenerSize(cameraId: Int): Int {
        return CameraManager.getInstance().getOrCreateHelperWithId(cameraId).listerNerSize()
    }

    /**
     * 延迟关闭摄像头 避免长时间使用
     */
    private fun delyCloseCamera(cameraId: Int) {
        Handler(Looper.getMainLooper()).postDelayed({
            if (isPerviewIng(cameraId) && listenerSize(cameraId) == 0) {
                CameraManager.getInstance().releaseCamera(cameraId)
            }
        }, 5 * 1000)
    }

    fun isPerviewIng(front: Boolean): Boolean {
        return isPerviewIng(getCameraId(front))
    }


    private fun isPerviewIng(cameraId: Int): Boolean {
        val orCreateHelperWithId = CameraManager.getInstance().getOrCreateHelperWithId(cameraId)
        return  orCreateHelperWithId.isPerviewIng
    }

    fun releaseCamera(front: Boolean) {
        CameraManager.getInstance().releaseCamera(getCameraId(front))
    }

    fun startPerview(front: Boolean) {
        CameraManager.getInstance().perviewCamera(getCameraId(front))
    }

    fun stopPerview(front: Boolean) {
        CameraManager.getInstance().stopPerview(getCameraId(front))
    }

    fun takePic(front: Boolean) {
        CameraManager.getInstance().takePic(getCameraId(front))
    }


}